<?php
class Users_model extends CI_Model {

    var $id = '';
    var $alias = '';
    var $pass = '';
    var $name = '';
    var $id_rol = '';
    private $_tabla = 'users';

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    public function find($id)
    {
        $query = $this->db->where('id', $id)->get($this->_tabla);
        $rs = $query->result();
        if (isset($rs[0])) {
            return $rs[0];
        } 
        return false;
    }
        
    public function getAll()
    {
        $query = $this->db->query("SELECT u.id, u.alias, u.password, u.name, r.description FROM `users` as u, rols as r where u.status = 1 and u.id_rol =  r.id_rol");
        $rs = $query->result();
        return $rs;
    }
    
    public function search($alias = null, $name = null)
    {
        $query = $this->db;
        if (isset($alias) && $alias != '') {
            $query = $query->like($this->_tabla.'.alias', $alias);
        }
        if (isset($name) && $name != '') {
            $query = $query->like($this->_tabla.'.name', $name);
        }  
        $query = $query->join('rols', $this-> _tabla.".id_rol =rols.id_rol", 'On');
        $query = $query->where($this->_tabla.'.status', 1);
        $query = $query->get($this->_tabla);
        $rs = $query->result();
        return $rs;
    }
    
    public function add($data)
    {
        $this->db->insert($this->_tabla, $data);
        return true;
    }
    
    public function delete($id)
    {
        $this->db->where('id', $id);
        $this->db->update($this->_tabla, array('status' => 0));
        return true; 
    }
  
    public function update($id, $data)
    {
       /* if ($data['id'] == false) {
            unset($data['id']);
        }*/
        $this->db->where('id', $id);
        $this->db->update($this->_tabla, $data);
        return true;
    }
	
	public function detected_alias($alias){
		$query = $this->db->query("SELECT `alias` FROM `users` WHERE `alias`='".$alias."'");
		$rs = $query->num_rows();
        return $rs;
	}
}